package com.alex.algorithm.iguide.stack;

import java.util.Stack;

public class reverserWithRecursion {

    public static void main(String[] args) {
        Stack<Integer> s = new Stack<>();
        s.push(1);
        s.push(2);
        s.push(3);
        s.push(5);
        reverse(s);
        while (!s.isEmpty()){
            System.out.println(s.pop());
        }
    }

    private static void reverse(Stack<Integer> s) {
        if (s.isEmpty()) {
            return;
        }
        int v = getAndRemoveNext(s);
        reverse(s);
        s.push(v);
    }

    private static int getAndRemoveNext(Stack<Integer> s) {
        int v = s.pop();
        if (s.empty()) {
            return v;
        }else {
            int n = getAndRemoveNext(s);
            s.push(v);
            return n;
        }
    }
}
